import tkinter as tk
from tkinter import messagebox
import json
from src.types_pydantic.config_types import Ecu_UnitTestConfig_Type
from abc import ABC, abstractmethod
from src.globals import g_ic_debugger


class Lib_pos_Frame(tk.Frame, ABC):
    def __init__(self, master, rooter, config_file, pos_data_Type=None, pos_Toplevel_UI=None):
        super().__init__(master=master)
        self.ROOTER = rooter
        self.config_file = config_file
        self.const_init()

        self.unitTabInit()

    def const_init(self):
        # 单元测试位置列表
        self.LIB_UNIT_POS_LIST = []
        # 单元测试位置按钮函数字典
        self.LIB_UNIT_FUN_DICT = {}
        # 单元测试位置按钮字典
        self.LIB_UNIT_BTN_DICT = {}
        # 单元测试配置字典
        self.LIB_UNIT_DATA_DICT = {}

    def unitTabInit(self):
        self.unitTabDataInit()
        self.unitTabViewInit()

        # FIXME 最后对单个按钮进行特殊设置的方法，需要在子类中重新调用，否则会被子类中的方法覆盖掉
        # self.unitTab_btn_function_update()

    def unitTabDataInit(self):

        #  单元测试配置信息
        self.unitTab_UNIT_TEST_CONFIG_DICT: dict[str, Ecu_UnitTestConfig_Type] = {
        }
        MAX_ROW = 8
        MAX_COLUMN = 8
        for i in range(MAX_ROW):
            for j in range(MAX_COLUMN):
                position = f"{i}{j}"
                self.LIB_UNIT_POS_LIST.append(position)

        def default_unit_pos_btn_click(i, j):
            return lambda: messagebox.showinfo("提示", f"unit R{i}C{j}未定义")
        for pos in self.LIB_UNIT_POS_LIST:
            self.LIB_UNIT_FUN_DICT[pos] = default_unit_pos_btn_click

    def unitTabViewInit(self):
        for i, j in self.LIB_UNIT_POS_LIST:
            self.LIB_UNIT_BTN_DICT[f"{i}{j}"] = tk.Button(
                self, text=f'POS_R{i}C{j}', padx=10, pady=5, command=self.LIB_UNIT_FUN_DICT[f'{i}{j}'](i, j))
            self.LIB_UNIT_BTN_DICT[f"{i}{j}"].grid(row=i, column=j)

        #  调整行列最小间距
        col_count, row_count = self.grid_size()
        for col in range(col_count):
            self.grid_columnconfigure(col, minsize=110)
        for row in range(row_count):
            self.grid_rowconfigure(row, minsize=60)

    @abstractmethod
    def load_lib_config(self):
        # DOC 加载配置文件，在子类中通过打开配置文件来更新lib_config
        pass

    @abstractmethod
    def create_posBtns(self):
        '''
            在子类中实现，后续排查故障时候，可以有代码提示，提升运维效率
        '''
        pass
        '''
        for pos, item in self.pos_data_configs.items():
            self.LIB_UNIT_DATA_DICT[pos] = self.pos_data_Type(**item)
            g_ic_debugger(self.LIB_UNIT_DATA_DICT[pos])

            def btn_click(pos):
                return lambda: self.pos_Toplevel_UI(
                    self, self.ROOTER, self.LIB_UNIT_DATA_DICT[pos])
            # 通过函数执行每次得到一个闭包，这样就可以在每次点击时，得到不同的pos
            # 否则，所有的按钮都会指向最后一个pos
            self.LIB_UNIT_FUN_DICT[pos] = btn_click(pos)

            self.LIB_UNIT_BTN_DICT[pos].config(
                text=item['name'], background='orange', command=self.LIB_UNIT_FUN_DICT[pos])
        '''

    def unitTab_btn_function_update(self):
        for pos in self.LIB_UNIT_POS_LIST:
            update_function = getattr(
                self, f'unit_function_update_{pos}', None)
            if update_function:
                update_function()

    def unit_function_update_76(self):
        pos = '76'

        def func():
            def wrapper():
                messagebox.showinfo('提示', f'unit{pos}-update......')
            return wrapper
        # FIXME 每次都通过函数func()获取新的函数闭包，否则会一直使用同一个函数闭包
        self.LIB_UNIT_FUN_DICT[pos] = func()
        self.LIB_UNIT_BTN_DICT[pos].config(
            command=self.LIB_UNIT_FUN_DICT[pos], background='pink')

    def unit_function_update_77(self):
        pos = '77'

        def func():
            def wrapper():
                messagebox.showinfo('提示', f'unit{pos}-update......')
            return wrapper
        # FIXME 每次都通过函数func()获取新的函数闭包，否则会一直使用同一个函数闭包
        self.LIB_UNIT_FUN_DICT[pos] = func()
        self.LIB_UNIT_BTN_DICT[pos].config(
            command=self.LIB_UNIT_FUN_DICT[pos], background='pink')
